use std::time::{Duration, Instant};

fn to_upper_naive(str_bytes: &mut [u8]) {
    for i in 0..str_bytes.len() {
        if str_bytes[i] >= b'a' && str_bytes[i] <= b'z' {
            str_bytes[i] -= 32;
        }
    }
}
fn to_upper_no_branch(str_bytes: &mut [u8]) {
    for i in 0..str_bytes.len() {
        str_bytes[i] -= 32 * (str_bytes[i] >= b'a' && str_bytes[i] <= b'z') as u8;
    }
}

fn to_upper_if_let(str_bytes: &mut [u8]) {
    for i in 0..str_bytes.len() {
        str_bytes[i] -= if let b'a'..=b'z' = str_bytes[i] {
            32
        } else {
            0
        };
    }
}

fn main() {
    const test_time: usize = 1000000;
    let mut test_string = String::from(
        r#"OXFORD CHARTER TOWNSHIP, Mich. — Students at Oxford High School in suburban Detroit rushed for cover and barricaded classroom doors with chairs when they heard the first gunshots on Tuesday afternoon. Within five minutes, the authorities said, a 15-year-old sophomore at the school had shot 11 people, killing three of his fellow students and leaving others with critical injuries.

The authorities identified the dead as Hana St. Juliana, 14; Madisyn Baldwin, 17; and Tate Myre, 16, who died in a sheriff’s squad car while on the way to a hospital. The injured students ranged in age from 14 to 17, officials said, including three who were in critical condition and another who was in serious condition. The only adult who was shot, a 47-year-old teacher, had been discharged from a hospital."#,
    );
    let mut str_bytes = test_string.into_bytes();

    let mut start;
    let mut duration;
    start = Instant::now();
    for i in 0..test_time {
        to_upper_naive(&mut str_bytes);
    }
    duration = start.elapsed();

    println!("Time elapsed in to_upper_naive() is: {:?}", duration);

    start = Instant::now();
    for i in 0..test_time {
        to_upper_no_branch(&mut str_bytes);
    }
    duration = start.elapsed();

    println!("Time elapsed in to_upper_no_branch() is: {:?}", duration);

    start = Instant::now();
    for i in 0..test_time {
        to_upper_if_let(&mut str_bytes);
    }
    duration = start.elapsed();

    println!("Time elapsed in to_upper_if_let() is: {:?}", duration);

    //let s = String::from_utf8(str_bytes).unwrap();
    //println!("{}", &s);
}
